package com.hudongwx.sadpw.fragment.adapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.activity.FoodMenuActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

/**
 * Author:Literary
 * DateTime:16-12-13 下午4:49
 */
public class GridAdapter extends BaseAdapter {
    JSONArray grid;
    Context context;
    private String TAG="GridAdapter";
    public GridAdapter(JSONArray grid, Context context) {
        this.grid = grid;
        this.context = context;
    }

    @Override
    public int getCount() {
        return grid.length();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder;
        if (null==convertView){
            viewHolder=new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_home1, null);
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.home1_iv);
            viewHolder.textView= (TextView) convertView.findViewById(R.id.home1_tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (MyViewHolder) convertView.getTag();
        }
        try {
            x.image().bind(viewHolder.imageView,grid.getJSONObject(position).getString("IconUrl"));
            viewHolder.textView.setText(grid.getJSONObject(position).getString("Alias"));
            viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, FoodMenuActivity.class);
                    intent.putExtra("type",position);
                    context.startActivity(intent);
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "getView:imageView: "+viewHolder.imageView);
        return convertView;
    }
    class MyViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
